import { DomMessageInstance } from "../../utils/DomMessage"
import { getCurrentPageInfo, dealAllTypeData, saveWebSpiderDataV2 } from "./generate"

export const useSpecificData = () => {
/** 点击页面中 获取数据 按钮时触发的方法 */
  const getData = async () => {
    try {
      const reqData = getCurrentPageInfo()
      console.log('reqData', reqData)
      if(!reqData) return Promise.reject(new Error('获取页面数据失败'))
      const {statement, pageName} = reqData
      const saveData:any = dealAllTypeData(reqData)
      console.log('saveData', saveData)
      if(!saveData) return Promise.reject(new Error('处理页面数据失败'))

      await saveWebSpiderDataV2(saveData)

      DomMessageInstance.success({content: `数据已保存成功 <br> ${statement} <br> ${pageName}`})
    }catch(e) {
      console.error(e)
    }
  }

  return {
    getData
  }
}